package com.pi.shisan.web.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Component;
import com.pi.shisan.service.exception.BusinessException;
import com.pi.shisan.web.exception.ExceptionPackage;

@Component
@Aspect
public class ExceptionJoinPoint {

    /**
     * 切入点方法
     */
    @Pointcut("execution(public * *..controller..*Controller.*(..))")
    public void afterError() {
    }

    /**
     * 抓住任何抛异常的Controller的入参并通过全局异常处理类打印出来。
     * @param jp
     * @param ex
     */
    @AfterThrowing(pointcut = "afterError()", throwing = "ex")
    public void throwss(JoinPoint jp, Exception ex){
        if(ex instanceof BusinessException){
            throw (BusinessException)ex;
        }
        if(ex instanceof ExceptionPackage){
            throw (ExceptionPackage)ex;
        }

        if (ex instanceof HttpMessageNotReadableException) {
            throw (HttpMessageNotReadableException) ex;
        }

        Object[] args = jp.getArgs();
        throw new ExceptionPackage(args,ex);
    }

}